if (!window.XMLHttpRequest)
{
	var XMLHttpRequest = function()
	{
		try { return new ActiveXObject("MSXML3.XMLHTTP") } catch(e) {}
		try { return new ActiveXObject("MSXML2.XMLHTTP.3.0") } catch(e) {}
		try { return new ActiveXObject("MSXML2.XMLHTTP") } catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
	}
}

var sonetChatXmlHttpAdd = new XMLHttpRequest();
var sonetChatXmlHttpGet = new XMLHttpRequest();
var sonetChatXmlHttpOpt = new XMLHttpRequest();

BX.bind(window, "load", sonet_chat_onload);
BX.bind(window, "resize", sonet_chat_onresize);
BX.bind(document, "keypress", sonet_chat_onkeypress);

/*if(BX.browser.IsIE())
{
	BX.bind(document, "focusin", sonetSetFocus);
	BX.bind(document, "focusout", sonetLostFocus);
}
else
{
	BX.bind(window, "focus", sonetSetFocus);
	BX.bind(window, "blur", sonetLostFocus);
}*/

var sonetChatTout;
var lastShowMessID = 0;
var sonetChatFocus = true;
responsible_id = 0;

function sonet_chat_msg_add()
{

	var textarea = document.getElementById('post_message_id');
	
	if (!textarea.value || sonetChatXmlHttpAdd.readyState % 4)
		return;

	var saveText;

	SocNetShowWaitWindow();

	sonetChatXmlHttpAdd.open("POST", sonetChatMsgAddPath, true);
	sonetChatXmlHttpAdd.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	sonetChatXmlHttpAdd.onreadystatechange = function()
	{
		if(sonetChatXmlHttpAdd.readyState == 4)
		{
			if(sonetChatXmlHttpAdd.status == 200)
			{
				if(BX.util.trim(sonetChatXmlHttpAdd.responseText).length > 0)
				{
					sonetChatMsgs += '<br /><b>' + messageTrSystem + '</b><br />' + sonetChatXmlHttpAdd.responseText + '<br />';
					sonet_chat_out_msgs(true);
					//sonet_play_sound('system');
					textarea.value = saveText;
				}
			}
			else
			{
				sonetChatMsgs += '<br /><b>' + messageTrSystem + '</b><br />' + messageNetworkError + '<br />';
				sonet_chat_out_msgs(true);
				//sonet_play_sound('system');
				textarea.value = saveText;
			}
			BX('post_message_button').disabled = false;
			sonet_chat_msgs_get();
		}
	}

	sonetChatXmlHttpAdd.send(sonetChatSessid
		+ "&data=" + encodeURIComponent(textarea.value)
		+ "&user_id=" + curUserID
		+ "&stream_id=" + stream_id
	);

	saveText = textarea.value;
	textarea.value = '';
	BX('post_message_button').disabled = true;
	textarea.focus();

}
function sonet_chat_msgs_get()
{
	clearTimeout(sonetChatTout);
	sonetChatTout = setTimeout("sonet_chat_msgs_get();", Math.round(1000 * sonetChatTimeout));
	if (sonetChatXmlHttpGet.readyState % 4)
	{
		SocNetCloseWaitWindow();
		return;
	}

	sonetChatXmlHttpGet.open(
		"get",
		sonetChatMsgGetPath + "?"
			+ sonetChatSessid
			+ ((parseInt(stream_id)>0) ? '&stream_id='+stream_id : '')
			+ ((parseInt(lastShowMessID)>0) ? '&last_id='+lastShowMessID : '')
			+ ((parseInt(responsible_id)>0) ? '&r='+responsible_id : '')
			+ ((parseInt(curUserID)>0) ? '&u='+curUserID : '')
	);

	sonetChatXmlHttpGet.onreadystatechange = function()
	{
		if (sonetChatXmlHttpGet && sonetChatXmlHttpGet.readyState == 4 && sonetChatXmlHttpGet.status == 200)
		{
			SocNetCloseWaitWindow();

			if (sonetChatXmlHttpGet.responseText.length > 0)
			{
				var response = eval('(' + sonetChatXmlHttpGet.responseText + ')'); 

				if (response["ONLINE"])
				{
					BX.cleanNode("users_online");
					var i = 0;
					while (response["ONLINE"][i])
					{
						node = response["ONLINE"][i];
						
						
						td = BX.create(
							"div", 
							{
								props: 
								{
									className: "user"
								},
								children: [
									BX.create('div', {html: node["PHOTO"]}),
									//BX.create('SPAN', {html: node["NAME"]})
								]
							}
						);

						BX("users_online").appendChild(td);
						i++;
					}
				}

				if (response["RESPONSIBLE"])
				{
					BX("socnet-user-name").innerHTML = response["RESPONSIBLE"]["NAME"];
					BX("bx-photo").innerHTML = response["RESPONSIBLE"]["PHOTO"];
					responsible_id = response["RESPONSIBLE"]["ID"];
				}
				if (response["MESSAGES"])
				{
					var i = 0;
					while (response["MESSAGES"][i] && response["MESSAGES"][i].ID > 0)
					{
						var mess = response["MESSAGES"][i];
						
						sonetChatMsgs += '<br /><span class="socnet-chat-incoming"><div class="socnet-chat-icon"></div><b>'
									+ mess.USER_NAME + ((mess.IS_SUPPORT == "Y")? '<span style="color:green;">   (support)</span>'  : '' )
									+ '</b><br style="clear:both;"/>' 
									+ '<span class="socnet-chat-date">' + mess.DATE_CREATE + '</span> ' + mess.MESSAGE + '</span><br />';
							
						i++;
						
					}		
					if (i > 0)
					{
						lastShowMessID = response["MESSAGES"][i-1].ID;
						bShowMess = true;
						sonet_chat_out_msgs();
					}
				}
			}
		}
	}
	sonetChatXmlHttpGet.send(null);
}
function sonet_chat_onload()
{
	var el = document.getElementById('post_message_id');
	if(el)
	{
		el.focus();
		bNeedUpdOnline = true;
		sonet_chat_reset();
	}
	sonet_chat_onresize();
}
function sonet_chat_reset()
{
	sonetChatPtr = sonetChatNowDate;

	sonetChatMsgs = "";

	clearTimeout(sonetChatTout);
	sonetChatXmlHttpAdd.abort();
	sonetChatXmlHttpGet.abort();

	SocNetShowWaitWindow();
	sonetChatTout = setTimeout("sonet_chat_msgs_get("+startShowMessageID+");", 1);
}

function SocNetWaitOnKeyPress(e)
{
	if(!e) e = window.event;
	if(!e) return;
	if(e.keyCode == 27)
		SocNetCloseWaitWindow();
}
function sonet_chat_onkeypress(e)
{
	if(!e) e = window.event;
	if(!e) return;
	if(e.keyCode == 27)
		window.close();
}
function sonet_chat_onresize()
{
	var div0 = document.getElementById('socnet_chat_body');
	var div1 = document.getElementById('socnet_chat_info');
	var div2 = document.getElementById('sonet_chat_messages');
	if(!div2)
		return;
	var div3 = document.getElementById('socnet_chat_form');
	var div_selectors = document.getElementById('socnet_chat_selectors');
	//var div_group = document.getElementById('socnet_user_list');
	div0.style.height = document.body.clientHeight + 'px';

	var d1 = BX.style(div0, 'padding-top');
	var d2 = BX.style(div0, 'padding-bottom');
	var d3 = BX.style(div2, 'margin-top');
	var d4 = BX.style(div3, 'margin-top');

	var body_margin = parseInt(d1? d1:BX.style(div0, 'paddingTop')) +
		parseInt(d2? d2:BX.style(div0, 'paddingBottom'));

	var h =  document.body.clientHeight - div1.offsetHeight - div3.offsetHeight 
		- body_margin
		- parseInt(d3? d3:BX.style(div2, 'marginTop')) 
		- parseInt(d4? d4:BX.style(div3, 'marginTop'));
	div2.style.height = (h < 30? 30:h) + 'px';
	
	//div_group.style.height = document.body.clientHeight - body_margin - div_selectors.offsetHeight - 3 + 'px';
}

function SocNetShowWaitWindow()
{
	SocNetCloseWaitWindow();

	var obWndSize = BX.GetWindowSize();

	var div = document.body.appendChild(document.createElement("DIV"));
	div.id = "socnet_wait_window_div";
	div.className = "socnet-waitwindow";
	div.style.right = (5 - obWndSize.scrollLeft) + 'px';
	div.style.top = obWndSize.scrollTop + 5 + "px";

	if(BX.browser.IsIE())
	{
		var frame = document.createElement("IFRAME");
		frame.src = "javascript:''";
		frame.id = "socnet_wait_window_frame";
		frame.className = "socnet-waitwindow";
		frame.style.width = div.offsetWidth + "px";
		frame.style.height = div.offsetHeight + "px";
		frame.style.right = div.style.right;
		frame.style.top = div.style.top;
		document.body.appendChild(frame);
	}
	//jsUtils.addEvent(document, "keypress", SocNetWaitOnKeyPress);
}
function SocNetCloseWaitWindow()
{
	BX.unbind(document, "keypress", SocNetWaitOnKeyPress);

	var frame = BX("socnet_wait_window_frame");
	if(frame)
		frame.parentNode.removeChild(frame);

	var div = BX("socnet_wait_window_div");
	if(div)
		div.parentNode.removeChild(div);
}


function sonet_load_last_char()
{
	sonetChatMsgs = "";
	sonetChatPtr = sonetChatLastDate;
	SocNetShowWaitWindow();
	sonet_chat_msgs_get();
	document.getElementById('post_message_id').focus();
}

function sonet_chat_out_msgs(bShowNotice)
{
		var o = document.getElementById('sonet_chat_messages');

	//if srollbar in the bottom, lets scroll
	var bScroll = (o.scrollHeight <= o.clientHeight || o.scrollTop > o.scrollHeight-o.clientHeight-10);

	o.innerHTML = sonetChatMsgs;

	if(bScroll)
		o.scrollTop = o.scrollHeight-o.clientHeight+100;
		
	if(!sonetChatFocus && bShowNotice)
	{
		sonetChatNewTitle = messageNewMessage;
		sonet_chat_settitle();
	}
}

/* Form functions */
function init_form(form)
{
	if (typeof(form) != "object")
		return false;
	/*if (!oForumForm[form.id] || typeof(oForumForm[form.id]) != "object")
	{
		oForumForm[form.id] = new PostForm(form);
		oForumForm[form.id].Init(form);
	}*/
	return;
}